/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package buisness;

import dal.FriendsDAL;
import dal.UsersDAL;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import models.Users;

/**
 *
 * @author gabrielle
 */
public class UserCreationServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.sendRedirect("./login.jsp");
    }


    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
 

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException 
    {
        //on récupère tous les attributs et on ajoute dans bdd
        //check que tous les paramètres existent que pas nul
        //crypter mdp dans bdd
        String firstname = request.getParameter("firstname");
        String lastname = request.getParameter("lastname");
        
        String password = request.getParameter("password");
        //String cryptPassw=Crypt.crypt(password);
        String username = request.getParameter("username");
        String mail = request.getParameter("mail");
        if(firstname!=null && lastname != null && password != null && username != null && mail!=null)
        {
            //il faut set un type de message pour envoyer au bon endroit après création d'utilisateur
            //g choisi wall mais c aléatoire.
            request.getSession(true).setAttribute("type", "wall");
            FriendsDAL.insertFriend(new models.Friends(0, username));
            Users user=new Users(FriendsDAL.getFriendByUsername(username).getIdFriends(), username, password, firstname, lastname, new Date(System.currentTimeMillis()), mail, 0, "", "", "", "");
            UsersDAL.insertUser(user);
            
            request.getSession().setAttribute("username", user.getUsername());
            response.sendRedirect("./messages");
        }
        else
        {
            response.sendRedirect("./login.jsp");
        }
        
    }

}
